package com.fili.gui;

import com.fili.core.sources.Sources;
import com.fili.core.sources.MappedSource;
import com.fili.gui.sources.SourceTree;
import com.fili.gui.sources.SourceView;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Nov 17, 2006
 * Time: 11:20:31 AM
 * Revision: 0
 */
public class MainFrame extends JFrame implements PropertyChangeListener
{
    private JSplitPane hSplitter;
    private JSplitPane vSplitter;

    private SourceTree sourceTree;
    private SourceView textPane;

    public MainFrame()
    {
        setSize(800, 600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        getContentPane().setLayout(new BorderLayout());


        textPane = new SourceView();
//        textPane.setEditable(false);
        vSplitter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JPanel(), textPane);
        hSplitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT, vSplitter, new JPanel());

        getContentPane().add(hSplitter, BorderLayout.CENTER);
        setVisible(true);

        vSplitter.setDividerLocation(0.25);
        hSplitter.setDividerLocation(0.75);
        loadSources("D:\\work\\projects\\main\\SPTM\\trunk\\build\\Nokia_N80\\temp\\sources");
    }

    private void loadSources(String path)
    {
        try
        {
            sourceTree = new SourceTree(new Sources(path));
            sourceTree.addPropertyChangeListener(SourceTree.EVENT_OPEN, this);
            vSplitter.setLeftComponent(sourceTree);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    public void propertyChange(PropertyChangeEvent evt)
    {
        System.out.println("Loading...");
        StringBuffer buf = new StringBuffer();

        MappedSource ms = (MappedSource) evt.getNewValue();
/*        for (int i = 0; i < ms.getSize(); i++)
        {
            if (ms.getMapping(i) >= 0)
                buf.append(">  ");
            else
                buf.append("x  ");
            buf.append(ms.getLine(i)).append("\n");
        }*/

        textPane.setSource(ms);
    }
}
